package com.cily.utils.t_webrtc;

import android.content.Intent;
import android.os.CountDownTimer;

import com.cily.utils.app.ac.BaseActivity;
import com.cily.utils.app.event.Event;
import com.cily.utils.app.utils.SpUtils;
import com.cily.utils.base.StrUtils;
import com.cily.utils.log.L;
import com.cily.utils.media.Player;
import com.cily.utils.t_webrtc.impl.ObserverImpl;
import com.cily.utils.t_webrtc.utils.ObserverUtils;
import com.cily.utils.t_webrtc.utils.WsUtils;

/**
 * user:cily
 * time:2017/5/3
 * desc:
 */

public abstract class BaseAc3 extends BaseActivity implements ObserverImpl {

    @Override
    protected void onStart() {
        super.onStart();

        String userRoom = SpUtils.getStr(this, Conf.USER_ROOM, null);
//        if (StrUtils.isEmpty(userRoom)){
//            if (this instanceof LoginAc){
//
//            }else{
//                showToast("未登录，请先登录");
//                Intent i = new Intent(this, LoginAc.class);
//                startActivity(i);
//            }
//        }else{
//            WsUtils.startWs(this, userRoom);
//        }
        ObserverUtils.getInstance().regist(this);
    }

    @Override
    protected void onStop() {
        super.onStop();

        ObserverUtils.getInstance().unregist(this);

        cancelTimer();
        stop();
    }

    @Override
    public void doEvent(Event e) {
        L.i(TAG, "收到消息：" + e.obj);
        if (e.what == Conf.WS_MSG){
            doResponse(e.obj);
        }else if (e.what == Conf.WS_CLOSED){
            Intent i = new Intent(this, LoginAc.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.putExtra(Conf.INTENT_FORCE_CLOSE, true);
            i.putExtra(Conf.INTENT_CONN_EXCEPTION, "未登录或连接已断开");
            startActivity(i);
            finish();
        }
    }

    protected void doResponse(Object b){}



    CountDownTimer timer;
    protected void startTimer(){
        if (timer == null){
            timer = new CountDownTimer(Conf.TIMER, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {

                }

                @Override
                public void onFinish() {
                    stop();
                    cancelTimer();
                    timerFinish();
                }

            };
        }
        timer.cancel();
        timer.start();
        play();
    }

    protected void timerFinish(){

    }

    protected void cancelTimer(){
        if (timer != null){
            timer.cancel();
        }
    }

    protected void play(){
        if (BuildConfig.DEBUG && false){
            showToast("播放音乐");
        }else{
            if (player == null){
                player = new Player();
            }
            player.start(this, R.raw.waiting_0, true);
        }
    }

    protected void stop(){
        if (BuildConfig.DEBUG && false){
            showToast("停止播放");
        }else{
            if (player != null){
                player.stop();
            }
        }
    }

    Player player;
    protected void release(){
        if (player != null){
            player.release();
        }
    }
}
